//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DigitalReplication.h"
using namespace Cephei::QL::Cashflows;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CDigitalReplication::CDigitalReplication (Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Replication::TypeEnum>^ t, Microsoft::FSharp::Core::FSharpOption<Double>^ gap) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phDigitalReplication = NULL;
#endif
        QuantLib::Replication::Type _t = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Replication::TypeEnum>::IsSome::get (t) ? (QuantLib::Replication::Type)t->Value : QuantLib::Replication::Type::Central); //10
        QuantLib::Real _gap = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (gap) ? (QuantLib::Real)ValueHelper::Convert (gap->Value) : 1e-4); //4
        _ppDigitalReplication = new boost::shared_ptr<QuantLib::DigitalReplication> (new QuantLib::DigitalReplication ( _t,  _gap ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CDigitalReplication::CDigitalReplication (boost::shared_ptr<QuantLib::DigitalReplication>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDigitalReplication = NULL;
#endif
	_ppDigitalReplication = &childNative;
    
}
Cephei::QL::Cashflows::CDigitalReplication::CDigitalReplication (QuantLib::DigitalReplication& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phDigitalReplication = NULL;
#endif
	_ppDigitalReplication = new boost::shared_ptr<QuantLib::DigitalReplication> (&childNative);
    
    _DigitalReplicationOwner = owner;
    
}

Cephei::QL::Cashflows::CDigitalReplication::CDigitalReplication (CDigitalReplication^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDigitalReplication = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDigitalReplication = new boost::shared_ptr<QuantLib::DigitalReplication> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::CDigitalReplication::CDigitalReplication (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDigitalReplication = NULL;
#endif
	if (!t->IsSubclassOf(CDigitalReplication::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CDigitalReplication::CDigitalReplication (QuantLib::Handle<QuantLib::DigitalReplication>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDigitalReplication = &childNative;
	_ppDigitalReplication = &static_cast<boost::shared_ptr<QuantLib::DigitalReplication>>(childNative.currentLink());
    
    _DigitalReplicationOwner = owner;
}
Cephei::QL::Cashflows::CDigitalReplication::CDigitalReplication (QuantLib::Handle<QuantLib::DigitalReplication> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDigitalReplication = &childNative;
	_ppDigitalReplication = &static_cast<boost::shared_ptr<QuantLib::DigitalReplication>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CDigitalReplication::CDigitalReplication (QuantLib::DigitalReplication childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDigitalReplication = NULL;
#endif
	_ppDigitalReplication = new boost::shared_ptr<QuantLib::DigitalReplication> (new QuantLib::DigitalReplication (childNative));
    
}
#endif

Cephei::QL::Cashflows::CDigitalReplication::~CDigitalReplication ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppDigitalReplication != NULL)
    {
	    delete _ppDigitalReplication;
        _ppDigitalReplication = NULL;
    }
}
Cephei::QL::Cashflows::CDigitalReplication::!CDigitalReplication ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppDigitalReplication != NULL)
    {
	    delete _ppDigitalReplication;
    }
}
QuantLib::DigitalReplication& Cephei::QL::Cashflows::CDigitalReplication::GetReference ()
{
    if (_ppDigitalReplication == NULL) throw REFNEW NativeNullException ();
	return **_ppDigitalReplication;
}
boost::shared_ptr<QuantLib::DigitalReplication>& Cephei::QL::Cashflows::CDigitalReplication::GetShared ()
{
    if (_ppDigitalReplication == NULL) throw REFNEW NativeNullException ();
	return *_ppDigitalReplication;
}
QuantLib::DigitalReplication* Cephei::QL::Cashflows::CDigitalReplication::GetPointer ()
{
    if (_ppDigitalReplication == NULL) throw REFNEW NativeNullException ();
	return &**_ppDigitalReplication;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DigitalReplication>& Cephei::QL::Cashflows::CDigitalReplication::GetHandle ()
{
	if (_phDigitalReplication == NULL)
	{
		_phDigitalReplication = new Handle<QuantLib::DigitalReplication> (*_ppDigitalReplication);
	}
	return *_phDigitalReplication;
}
#endif
bool Cephei::QL::Cashflows::CDigitalReplication::HasNative () 
{
	return (_ppDigitalReplication != NULL);
}

Double Cephei::QL::Cashflows::CDigitalReplication::Gap::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDigitalReplication)->gap ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Cashflows::Replication::TypeEnum Cephei::QL::Cashflows::CDigitalReplication::ReplicationType::get ()
{
    try
    {
    	QuantLib::Replication::Type _rv = (QuantLib::Replication::Type)(*_ppDigitalReplication)->replicationType ( );   
        QL::Cashflows::Replication::TypeEnum _nrv = (QL::Cashflows::Replication::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IDigitalReplication^ Cephei::QL::Cashflows::CDigitalReplication_Factory::Create (Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Replication::TypeEnum>^ t, Microsoft::FSharp::Core::FSharpOption<Double>^ gap)
{
    return REFNEW CDigitalReplication ( t,  gap);
}
